package com.speoki.springboot.controller;

import com.speoki.springboot.mapper.Course;
import com.speoki.springboot.mapper.Student;
import com.speoki.springboot.service.CourseService;
import com.speoki.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;


    /*设置student为首页*/
    @RequestMapping("/")
    public String hello() {
        return "redirect:/studentinfo";
    }


    @RequestMapping(value = "/studentinfo")
    public String queryStudentALL(Map<String, Object> map) {
        List<Student> all = studentService.getAll();
        map.put("student", all);

        return "StudentIndex";
    }


//更新
    @RequestMapping(value = "/queryStudentById")
    public Student queryStudentById(Integer id) {
        Student student = studentService.queryStudentById(id);
        return student;
    }

    @RequestMapping("/update")
    public String update() {
        return "redirect:/studentinfo";
    }
    @RequestMapping(value = "/updateStudentBySno")
    @Transactional  //涉及到更改，开启事务
    public String updateStudentName(HttpServletRequest request, Model model) {
        Integer id = Integer.valueOf(request.getParameter("sno"));
        Student student = studentService.queryStudentById(id);
        //首先找到学生
        String name = request.getParameter("sname");
        student.setSname(name);
        String age = request.getParameter("sage");
        model.addAttribute("ageInt", 0);
        if (!isDigit2(age)) {
            model.addAttribute("ageInt", 1);
            return "redirect:/update";
        }
        Integer sage = Integer.valueOf(age);
        student.setSage(sage);
        String sdept = request.getParameter("sdept");
        student.setSdept(sdept);
//      String ssex = request.getParameter("ssex");
//      if(ssex.equals("男")){
//          student.set
//      }


        Integer i = studentService.updateStudent(student);
        model.addAttribute("student", student);
        return "updateStudentBySno";
    }

    //查看课程信息
    @RequestMapping(value = "/courseinfo")
    public String queryCourseALL(Map<String, Object> map) {
        List<Course> all = courseService.getAll();
        map.put("course", all);

        return "courseIndex";
    }
//根据姓和性别查找学生
    @RequestMapping("/selecttwo")
    public String selecttwo() {
        return "selectByPreAndSex";
    }


    @RequestMapping("/selectByPreAndSex")
    public String selectByPreAndSex(Map<String, Object> map, HttpServletRequest request) {
        String prename = request.getParameter("prename");
        Integer sex = Integer.valueOf(request.getParameter("ssex"));
        List<Student> preAndSex = studentService.getPreAndSex(prename, sex);
        map.put("preAndSex", preAndSex);
        return "showPreAndSex";
    }


    /*查找平均数*/
    @RequestMapping("/selectavg2")
    public Object selectavg2(Model model) {
        List<Map<String, Object>> courses = courseService.getavgCourse2();
        Map<String, Object> mm = new HashMap<>();
        Map<String, Object> ss = new HashMap<>();
        for (Map<String, Object> map : courses) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                ss.put(entry.getKey(), entry.getValue());
            }
            mm.put((String) ss.get("Cname"), ss.get("average"));
        }
        for (Map<String, Object> map : courses) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println(entry.getKey() + entry.getValue());
            }
        }

        model.addAttribute("course", mm);

        return "selectAvg";
    }

    //查看学生和选修课程
    @RequestMapping("/studentandcourse")
    public Object selectstudentCourse(Model model) {
        List<Map<String, String>> courses = courseService.GetStudentCourse();
        Map<String, String> mm = new HashMap<>();
        Map<String, String> ss = new HashMap<>();
        for (Map<String, String> map : courses) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                ss.put(entry.getKey(), entry.getValue());
                //sname:"",cname:"";
            }
            if (mm.containsKey(ss.get("sname"))) {
                //如果存在了该学生的信息
                mm.put(ss.get("sname"), mm.get(ss.get("sname")) + "," + ss.get("cname"));
            } else {
                mm.put((String) ss.get("sname"), ss.get("cname"));

            }
        }
        for (Map<String, String> map : courses) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                System.out.println(entry.getKey() + entry.getValue());
            }
        }

        model.addAttribute("course", mm);

        return "studentandcourse";
    }
    //调用controller中的方法
    public boolean isDigit2(String s) {
        int len = s.length();
        for (int i = 0; i < len; i++) {
            if (!Character.isDigit(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }



    //插入课程

    @RequestMapping("/insert")
    public String add(Model model) {
        return "insertCourse";
    }

    @RequestMapping(value = "/insertCourse")
    @Transactional
    public String insertCourse(HttpServletRequest request, Model model) {
        Integer cno = Integer.valueOf(request.getParameter("cno"));
        Course course1 = courseService.queryCourseById(cno);
        System.out.println(course1);
        if (course1 != null) {
            return "Inserterror";
        }
        String cname = request.getParameter("cname");
        Integer ccredit = Integer.valueOf(request.getParameter("ccredit"));
        Integer cpno = Integer.valueOf(request.getParameter("cpno"));
        Course course = new Course();
        course.setCname(cname);
        course.setCpno(cpno);
        course.setCcredit(ccredit);
        course.setCno(cno);
        Integer result = courseService.insertCourse(course);
        if (result > 0) {
            model.addAttribute("course", course);
            return "CourseInsert";
        } else return "Inserterror";

    }

}
